local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/bs_decomposition_SS_ProdR_new_kl", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_I_b2* *_I_b3*
*drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2_`var' = `var'2 - ($sdv * `var'2se)
	gen highs2_`var' = `var'2 + ($sdv * `var'2se)
	gen lows3_`var' = `var'3 - ($sdv * `var'3se)
	gen highs3_`var' = `var'3 + ($sdv * `var'3se)
}

		drop if variables == "diff_tfpr" | variables == "dif_d_bar" | variables == "dif_a_bar"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		gen ordering = 1 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 2 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 3 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 4 if variables == "Dropping"
		sencode variables, gen(vars) gsort(ordering)
		gen vars1 = vars+.3
		gen vars2 = vars+.15
		twoway	(scatter Iy2 vars, mcolor(red) msymbol(square)) ///
				(rcap lows2_Iy highs2_Iy vars if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iy highs2_Iy vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iyn2 vars1, mcolor(red) msymbol(Sh)) ///
				(rcap lows2_Iyn highs2_Iyn vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2_Iyn highs2_Iyn vars1 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)4, angle(45) valuelabel) ///
				ylabel(,grid) ///
				xtitle("") ///
				legend(order(1 "Year FE" 4 "Industry and Year FE") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) 
				*title("`samplenametext'") subtitle("With Year Fixed Effects")
				
			graph export "$figures/Input_access_focus.png", as(png) replace width(1800) height(1000)
